/**
 * @overview 创建线
 * @author dengqiang
 * @update  2022.11.15
 */
class GWarPolyline {
  constructor() {
    window.GWarPolylineArray = []
  }

  //新增单条线
  add(paramObj) {
    const { name, parentName, option } = paramObj

    const centerPos = window.cesiumMapTool.getPolygonCenter([
      option.position[0],
      option.position[1],
    ])

    const position = window.cesiumMapTool.car3ToWGS84Two(centerPos)

    //原生方式
    const entity = {
      position: centerPos,
      polyline: {
        positions: option.position,
        material:
          (option.fillColor && Cesium.Color.fromCssColorString(option.fillColor)) ||
          Cesium.Color.RED, // 填充色
        clampToGround: option.clampToGround || false,
        width: option.width || 10, // 宽度
      },
    }

    //如果是箭头线 修改材质
    if (option?.isArrow) {
      entity.polyline.material = new Cesium.PolylineArrowMaterialProperty(
        (option.fillColor && { ...Cesium.Color.fromCssColorString(option.fillColor) }) || {
          ...Cesium.Color.RED.withAlpha,
        }
      )
    }

    //是否是贴地文字
    if (!option.clampToGround) {
      entity.label = {
        text: option.label.text || '',
        font: option.label.font || '18px san-serif',
        pixelOffset: option.label.labelOffset || new Cesium.Cartesian2(0, -10),
        distanceDisplayCondition:
          option.label.distanceDisplayCondition || new Cesium.DistanceDisplayCondition(0, 6000),
        disableDepthTestDistance: 10000,
        showBackground: true, //是否显示背景
        backgroundColor: option.label.backgroundColor || new Cesium.Color(0.165, 0.165, 0.165, 0.8), //背景颜色
        backgroundPadding: option.label.backgroundPadding || new Cesium.Cartesian2(7, 5), //背景边距
        fillColor: option.label.fillColor || Cesium.Color.WHITE, //填充色
        outlineColor: option.label.outlineColor || new Cesium.Color(0, 1, 1, 0), //外线颜色
        outlineWidth: option.label.outlineWidth || 1.0, //外线宽
      }
    } else {
      const rectang = Cesium.Rectangle.fromDegrees(
        position.lng -
          window.cesiumMapTool.trsfMeterToXY({ length: option.label.text.length }).longitude *
            0.5 *
            option.scale,
        position.lat -
          window.cesiumMapTool.trsfMeterToXY({ length: 1 }).latitude * 0.5 * option.scale,
        position.lng +
          window.cesiumMapTool.trsfMeterToXY({ length: option.label.text.length }).longitude *
            0.5 *
            option.scale,
        position.lat +
          window.cesiumMapTool.trsfMeterToXY({ length: 1 }).latitude * 0.5 * option.scale
      )

      entity.rectangle = {
        height: position.alt,
        coordinates: rectang,
        material: new Cesium.ImageMaterialProperty({
          image: this.drawText(option.label),
          transparent: true,
          color: { ...Cesium.Color.WHITE },
        }),
        rotation: Cesium.Math.toRadians(option.rotation || 0),
        stRotation: Cesium.Math.toRadians(option.rotation || 0),
      }
    }

    const polyline = window.map3DControl.viewer.entities.add(entity)

    polyline.attribute = option.attribute || {} //支持添加自定义属性
    polyline.attribute.name = name
    polyline.attribute.parentName = parentName
    polyline.attribute.position = option.position.map((item) =>
      window.cesiumMapTool.car3ToWGS84Two(item)
    )
    polyline.attribute.zxdjdwd = window.cesiumMapTool.getPolygonCenterTwo(
      polyline.attribute.position
    )

    polyline.isArrow = option?.isArrow //是否是箭头线
    const polylineObject = {
      name: name,
      parentName: parentName,
      polyline: polyline,
    }
    window.GWarPolylineArray.push(polylineObject)
    return polyline
  }

  //绘制文字
  drawText(style) {
    const text = style.text
    const c = document.createElement('canvas')
    const d = (text + '').length * style.fontSize
    c.width = d
    c.height = style.fontSize
    const ctx = c.getContext('2d')
    ctx.fillStyle = style.color
    ctx.font = 'bold ' + style.fontSize + 'px 微软雅黑' //设置字体
    ctx.textBaseline = 'hanging' //在绘制文本时使用的当前文本基线
    //绘制文本
    ctx.fillText(text, 0, 0)
    return c
  }

  //根据name移除单条线
  removeByName(name) {
    window.GWarPolylineArray.forEach((item, index) => {
      if (item.name == name) {
        window.map3DControl.viewer.entities.remove(item.polyline)
        window.GWarPolylineArray.splice(index, 1)
      }
    })
  }

  //根据parentName移除一批线
  removeByPatName(parentName) {
    const length = window.GWarPolylineArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GWarPolylineArray[i].parentName == parentName) {
        window.map3DControl.viewer.entities.remove(window.GWarPolylineArray[i].polyline)
        window.GWarPolylineArray.splice(i, 1)
      }
    }
  }

  //移除所有线
  removeAll() {
    window.GWarPolylineArray.forEach((item) => {
      window.map3DControl.viewer.entities.remove(item.polyline)
    })
    window.GWarPolylineArray = []
  }

  //根据name控制单条线的显隐
  setVisibleByName(name, boolean) {
    window.GWarPolylineArray.forEach((item) => {
      if (item.name == name) {
        item.polyline.show = boolean
      }
    })
  }

  //根据parentName控制一批线的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GWarPolylineArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.polyline.show = boolean
      }
    })
  }

  //根据name设置线的位置
  setPosition(name, newPosition, isSetLable) {
    window.GWarPolylineArray.forEach((item) => {
      if (item.name == name) {
        if (isSetLable) {
          item.polyline.position = window.cesiumMapTool.getPolygonCenter([
            newPosition[0],
            newPosition[1],
          ]) //标注位置
        }
        item.polyline.polyline.positions = newPosition
      }
    })
  }

  //根据name设置选中效果
  setActive(name, boolean, time = 100) {
    window.GWarPolylineArray.forEach((item) => {
      if (item.name == name) {
        //如果是箭头线，可以直接通过属性修改透明度
        if (item.polyline.isArrow) {
          window.cesiumMapTool.requestAnimationFrame(
            item.polyline.polyline.material.color,
            boolean,
            time,
            item.polyline
          )
        } else {
          window.cesiumMapTool.requestAnimationFrameTwo(
            item.polyline.polyline.material.color,
            boolean,
            time,
            item.polyline
          )
        }
      }
    })
  }
}

export default GWarPolyline
